<?php
    require_once 'include/util/helperFunctions.php';
    require_once 'include/model/Registration.php';
    require_once 'include/model/Event.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>De menapiërs inschrijvingsmodule</title>

        <link href="CSS/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div class="main">
        <?php
        $isPostCorrect = true;
        $errorMessage = "Er ging iets bijzonder mis. Stuur ons snel een mailtje!";

        dump($_POST, false);

        //
        // check vars
        //
        // check event
        if (isset ($_POST['event_id'])) $event = Event::loadEvent((integer) $_POST['event_id']);
        else $isPostCorrect = false;
        // check email
        if (!($isPostCorrect && isset ($_POST['inschr_em'])))
        {
            $errorMessage = "E-mailadres niet ingevuld";
            $isPostCorrect = false;
        }
        elseif (!preg_match("/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/", $_POST['inschr_em']))
        {
            $errorMessage = "E-mailadres is niet correct";
            $isPostCorrect = false;
        } else $email = $_POST['inschr_em'];
        // check lastname
        if (!($isPostCorrect && isset($_POST['inschr_fm'])))
        {
            $errorMessage = "Achternaam niet ingevuld";
            $isPostCorrect = false;
        } else $registrationName = $_POST['inschr_fm'];

        

        // was it ok?
        if ($isPostCorrect) {

            // main event
            $registration = $event->createRegistration();
            $registration->setEmail($email);

            $numberOfSupplementsForMainEvent = count($event->getSupplements());

            // process entries for main event
            for ($i = 0; $i < count($_POST['part_main_fn']); $i++) {
                dump($i, false);
                if(isset($_POST['part_main_vn'][$i]) && !empty($_POST['part_main_vn'][$i])
                        && isset($_POST['part_main_fn'][$i]) && !empty($_POST['part_main_fn'][$i])
                        && isset($_POST["part_soort_main"][$i]) && !empty($_POST['part_soort_main'][$i])){

                    $firstname = $_POST['part_main_vn'][$i];
                    $lastname = $_POST['part_main_fn'][$i];

                    $entry = $registration->newEntry();
                    $entry->setFirstname($firstname);
                    $entry->setLastname($lastname);
                    $entry->setPriceCategory(PriceCategory::loadPriceCategory($event, (integer) $_POST["part_soort_main"][$i]));

                    $supplements = array();
                    for ($j = 1; $j < $numberOfSupplementsForMainEvent + 1; $j++){
                        if($_POST['part_chb_'.$j][$i] != 0) $supplements[] = Supplement::loadSupplement((integer) $_POST['part_chb_'.$j][$i]);
                    }
                    $entry->setSupplements($supplements);
                } else {
                    $isPostCorrect = false;
                    $errorMessage = "Sommige namen zijn niet ingevuld";
                }
                dump(array("event", $i, $supplements), false);
            }

            $registration->save();

            // subevent
            if ($isPostCorrect && isset ($_POST['subevent_id'])){
                $subevent = Event::loadEvent((integer) $_POST['subevent_id']);
                $registrationSubevent = $subevent->createRegistration();
                $registrationSubevent->setEmail($email);

                // process entries for subevent
                for ($i = 0; $i < count($_POST['part_sub_fn']); $i++) {
                    if(isset($_POST['part_sub_vn'][$i]) && !empty($_POST['part_sub_vn'][$i])
                            && isset($_POST['part_sub_fn'][$i]) && !empty($_POST['part_sub_fn'][$i])
                            && isset($_POST["part_soort_sub"][$i])&& !empty($_POST['part_soort_sub'][$i]) ){

                        $firstname = $_POST['part_sub_vn'][$i];
                        $lastname = $_POST['part_sub_fn'][$i];

                        $entry = $registrationSubevent->newEntry();
                        $entry->setFirstname($firstname);
                        $entry->setLastname($lastname);
                        $entry->setPriceCategory(PriceCategory::loadPriceCategory($subevent, (integer) $_POST["part_soort_sub"][$i]));

                        $supplements = array(); 
                        for ($j = $numberOfSupplementsForMainEvent; $j < $numberOfSupplementsForMainEvent + count($subevent->getSupplements()) + 1; $j++) {
                            if(!empty($_POST['part_chb_'.$j][$i])) $supplements[] = Supplement::loadSupplement((integer) $_POST['part_chb_'.$j][$i]);
                        }
                        $entry->setSupplements($supplements);
dump(array("subevent", $i, $supplements), false);
                    } else {
                        $isRegistrationSuccesfull = false;
                        $errorMessage = "Sommige namen zijn niet ingevuld";
                    }
                }

                $registrationSubevent->save();

            }
        }

        if ($isPostCorrect){
        ?>

        <img src="http://i550.photobucket.com/albums/ii416/mom4jazz/happy/lolcat-happy.jpg" alt="Its a happie!" />

        <?php
        }
        // else: something is wrong, let's show a funny errormessage.
        else
        { ?>

        <img src="http://forum.ntreev.net/pangya/cfs-filesystemfile.ashx/__key/CommunityServer.Discussions.Components.Files/20/8306.LolCat-Fail.jpg" alt="Fail!" />
        <?php
        echo "<p class=\"error\">$errorMessage</p>";
        }
        ?>
        <p><a href="index.php">Ga terug</a></p>
        </div>
    </body>
</html>
